package com.usian.service.impl;

import com.usian.MD5Utils;
import com.usian.mapper.TbUserMapper;
import com.usian.pojo.TbUser;
import com.usian.pojo.TbUserExample;
import com.usian.redis.RedisClient;
import com.usian.service.SSOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class SSOServiceImpl implements SSOService {

    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private RedisClient redisClient;

    @Value("${USER_INFO}")
    private String USER_INFO;

    @Override
    public boolean checkUserInfo(String checkValue, int checkFlag) {
//        查询条件
        TbUserExample userExample = new TbUserExample();
        TbUserExample.Criteria criteria = userExample.createCriteria();
//        判断  checkFlag  1 username  2 phone
        if (checkFlag == 1) {
            criteria.andUsernameEqualTo(checkValue);
        }else if (checkFlag == 2){
            criteria.andPhoneEqualTo(checkValue);
        }
//        查询  有结果  true  无结果  false
        List<TbUser> tbUsers = tbUserMapper.selectByExample(userExample);
        if (tbUsers.size()>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean userRegister(TbUser tbUser) {
//        MD5  密码加密
        String password = MD5Utils.digest(tbUser.getPassword());
        tbUser.setPassword(password);
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());
        int selective = tbUserMapper.insertSelective(tbUser);
        if (selective > 0) {
            return true;
        }
        return false;
    }

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public HashMap userLogin(String username, String password) {
//        验证用户名和密码
        String pwd = MD5Utils.digest(password);
        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(pwd);
        List<TbUser> tbUsers = tbUserMapper.selectByExample(example);
        if (tbUsers.size()>0){
//            用户名和密码校验通过用户登录成功
            TbUser tbUser = tbUsers.get(0);
//            1：生成一个token  UUID
            String token = UUID.randomUUID().toString();
//            2：用户参数存到redis   key   token   value  用户信息   map
            redisClient.set(USER_INFO+":"+token,tbUser);
            redisClient.expire(USER_INFO+":"+token,86400);
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("token",token);
            hashMap.put("userid", tbUser.getId().toString());
            hashMap.put("username", tbUser.getUsername());
            return hashMap;
        }
        return null;
    }

    /**
     * 更新redis中token的有效期
     *
     * @param token
     * @return
     */
    @Override
    public boolean getUserByToken(String token) {
        TbUser tbUser = (TbUser)redisClient.get(USER_INFO + ":" + token);
        if (tbUser!=null){
            return redisClient.expire(USER_INFO+":"+token,86400);
        }
        return false;
    }

    /**
     * 退出登录
     *
     * @param token
     * @return
     */
    @Override
    public boolean logOut(String token) {
        return redisClient.del(USER_INFO + ":" + token);
    }
}
